<template>
  <div>
    <el-form ref="formData" :model="formData" :rules="rules" label-position="right" label-width="90px">
      <el-form-item label="网站名称" prop="webName">
        <el-input placeholder="轮播名称" v-model="formData.webName"/>
      </el-form-item>
      <el-form-item label="站长" prop="nickname">
        <el-input placeholder="站长" v-model="formData.nickname"/>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input placeholder="邮箱" v-model="formData.email"/>
      </el-form-item>
      <el-form-item label="联系方式">
        <el-input placeholder="联系方式" v-model="formData.phone"/>
      </el-form-item>
      <el-form-item label="开发人员">
        <el-input placeholder="开发人员,多人使用逗号(,)隔开" v-model="formData.devName"/>
      </el-form-item>
      <el-form-item label="版权所有" prop="copy">
        <el-input placeholder="版权所有" v-model="formData.copy"/>
      </el-form-item>
      <el-form-item label="网站介绍1" prop="profile1">
        <el-input placeholder="网站介绍1" maxlength="40" show-word-limit type="textarea" v-model="formData.profile1"/>
      </el-form-item>
      <el-form-item label="网站介绍2" prop="profile2">
        <el-input placeholder="网站介绍2" maxlength="40" show-word-limit type="textarea" v-model="formData.profile2"/>
      </el-form-item>
      <el-form-item label="网站logo" prop="logo">
        <el-upload :action="uploadUrl" :headers="uploadHeader" :limit="1" list-type="picture-card" ref="uploadLogo" :file-list="logoList" :on-exceed="onExceed" :on-success="logoSuccess" :before-upload="beforeUpload">
          <i class="el-icon-plus"></i>
          <div slot="file" slot-scope="{file}">
            <img :src="file.url" alt="" class="el-upload-list__item-thumbnail">
            <div class="el-upload-list__item-actions">
          <span @click="showImage(file)">
            <i class="el-icon-zoom-in"></i>
          </span>
              <span @click="removeLogo(file)">
            <i class="el-icon-delete"></i>
          </span>
            </div>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="二维码">
        <el-upload :action="uploadUrl" :headers="uploadHeader" :limit="1" list-type="picture-card" ref="uploadQeCode" :file-list="qrCodeList" :on-exceed="onExceed" :on-success="qrCodeSuccess" :before-upload="beforeUpload">
          <i class="el-icon-plus"></i>
          <div slot="file" slot-scope="{file}">
            <img :src="file.url" alt="" class="el-upload-list__item-thumbnail">
            <div class="el-upload-list__item-actions">
          <span @click="showImage(file)">
            <i class="el-icon-zoom-in"></i>
          </span>
              <span @click="removeQrCode(file)">
            <i class="el-icon-delete"></i>
          </span>
            </div>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="update">提交</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="查看图片" :visible.sync="imageDialogVisible" v-if="imageDialogVisible" append-to-body>
      <img :src="showImageSrc" alt="" height="100%" width="100%">
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Website',
  data () {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + 'admin/upload',
      uploadHeader: {
        token: ''
      },
      logoList: [],
      qrCodeList: [],
      imageDialogVisible: false,
      showImageSrc: '',
      formData: {},
      rules: {
        webName: [{
          required: true,
          message: '网站名称不能为空',
          trigger: 'blur'
        }],
        nickname: [{
          required: true,
          message: '站长名称不能为空',
          trigger: 'blur'
        }],
        copy: [{
          required: true,
          message: '版权信息不能为空',
          trigger: 'blur'
        }],
        profile1: [{
          required: true,
          message: '网站介绍不能为空',
          trigger: 'blur'
        }],
        profile2: [{
          required: true,
          message: '网站介绍不能为空',
          trigger: 'blur'
        }],
        logo: [{
          required: true,
          message: '请选择logo',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    init () {
      this.$http.get('/admin/website/info').then(data => {
        this.formData = { ...data.data }
        this.logoList.push({ url: process.env.VUE_APP_BASE_API + 'img/' + this.formData.logo })
        this.qrCodeList.push({ url: process.env.VUE_APP_BASE_API + 'img/' + this.formData.qrCode })
      })
    },
    /* limit越界时触发 */
    onExceed () {
      this.$message.error('请先删除原图片')
    },
    removeLogo (file) {
      this.$confirm('确定删除图片吗？').then(() => {
        this.$refs.uploadLogo.handleRemove(file)
        this.formData.logo = ''
      })
    },
    removeQrCode (file) {
      this.$confirm('确定删除图片吗？').then(() => {
        this.$refs.uploadQeCode.handleRemove(file)
        this.formData.qrCode = ''
      })
    },
    /* 上传成功后的回调 */
    logoSuccess (res) {
      if (res.code === 200) {
        this.formData.logo = res.fileName
      } else {
        this.$message.error(res.msg)
      }
    },
    qrCodeSuccess (res) {
      if (res.code === 200) {
        this.formData.qrCode = res.fileName
      } else {
        this.$message.error(res.msg)
      }
    },
    /* 上传之前对格式和大小进行判断 */
    beforeUpload (file) {
      const isImage = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
      if (!isImage) {
        this.$message.error('请选择正确的图片格式！')
      }

      const isLt20M = file.size / 1024 / 1024 < 20
      if (!isLt20M) {
        this.$message.error('文件大小不能超过20M')
      }
      return isImage && isLt20M
    },
    showImage (file) {
      this.imageDialogVisible = true
      this.showImageSrc = file.url
    },
    update () {
      this.$refs.formData.validate(valid => {
        if (valid) {
          this.$http.post('admin/website/update', this.formData).then(() => {
            this.$alert(`${this.formData.userId ? '修改' : '添加'}成功`, { type: 'success' })
          })
        }
      })
    }
  },
  created () {
    this.$nextTick(() => {
      this.uploadHeader.token = this.$cookies.get('token')
      this.init()
    })
  }
}
</script>

<style scoped>

</style>
